package org.teziyou.ticket.tag;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

public class PageTag extends TagSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4602714761430358383L;
	
	private int pageNumber;
	private int totalPage;

	@Override
	public int doStartTag() throws JspException {
		try {
			pageContext.getOut().write(createHtml());
		} catch (IOException e) {
			e.printStackTrace();
		}
		return super.doStartTag();
	}
	
	public String createHtml() {
		String html = "<div class=\"Bnav\"><ul><div class=\"Lbtn\" onclick='turnpage(\"1\")'>首页</div>";
		if(pageNumber!=1) html+="<div class=\"Lbtn\" onclick='turnpage(\""+(pageNumber-1)+"\")'>上一页</div>";
		if(pageNumber<=5&&totalPage>=5) html+=createPageHtml(1, 6);
		else if(pageNumber<=5&&totalPage<=5) html+=createPageHtml(1, totalPage+1);
		else if(pageNumber>5&&pageNumber<totalPage-5) html+=createPageHtml(pageNumber-2, totalPage+3);
		else if(pageNumber>=totalPage-5) html+=createPageHtml(totalPage-4, totalPage+1);
		if(pageNumber!=totalPage) html+="<div class=\"Rbtn\" onclick='turnpage(\""+(pageNumber+1)+"\")'>下一页</div>";
		html+="<div class=\"Rbtn\" onclick='turnpage(\""+totalPage+"\")'>末页</div></ul></div>";
		return html;
	}
	
	public String createPageHtml(int start, int end) {
		String html = "";
		for(int i=start;i<end;i++) {
			String clazz = i==pageNumber?"class=\"click\"":"";
			html += "<a "+clazz+" onclick='turnpage(\""+i+"\")'><li style=\"display:inline-block;\">"+i+"</li></a>";
		}
		return html;
	}

	public int getPageNumber() {
		return pageNumber;
	}

	public void setPageNumber(int pageNumber) {
		this.pageNumber = pageNumber;
	}

	public int getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

}
